package com.gt.framework.security.wxauth;

import com.gt.common.core.domain.entity.SysUser;
import com.gt.common.core.domain.model.LoginUser;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.HashSet;
import java.util.Set;


/**
 * @author fishness
 * @version 1.0
 * @description
 * @date 2023-05-09
 */
public class WeChatAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String openid = (String)authentication.getPrincipal();
        SysUser sysUser = new SysUser();
        //加角色
        sysUser.setOpenId(openid);
        sysUser.setWxLogin(true);
        return new UsernamePasswordAuthenticationToken(createLoginUser(sysUser), authentication.getCredentials());
    }

    public UserDetails createLoginUser(SysUser user)
    {
        //fishness 需要时改造
        Set<String> menuPermission = new HashSet<>();
        return new LoginUser(user, menuPermission);
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return aClass.equals(WeChatAuthenticationToken.class);
    }
}
